package com.myh.exam.service;

import com.myh.exam.dao.UserDAO;
import com.myh.exam.dto.User;

import java.util.ArrayList;
import java.util.List;

public class UserService {
    private UserDAO userDAO = new UserDAO();

    //1.新增
    public int addUser(User user) {
        return userDAO.addUser(user);
    }

    //2.查询
    public List<User> selectUserList(User user, Integer start, Integer rows) {
        return userDAO.selectUserList(user, start, rows);
    }

    //3.更新
    public Integer updateUser(User user) {
        return userDAO.updateUser(user);
    }

    //4.删除
    public Integer delUser(Integer id) {
        return userDAO.delUser(id);
    }

    public User selectUser(Integer id) {
        User user = new User();
        user.setId(id);
        List<User> users = userDAO.selectUserList(user, 0, 1);
        if (users.size() == 0) {
            return null;
        }

        return users.get(0);
    }

    public User selectUser(String name, String passwd) {
        User user = new User();
        user.setName(name);
        List<User> users = userDAO.selectUserList(user, 0, 1);
        if (users.size() == 0) {
            return null;
        }
        if (!passwd.equals(users.get(0).getPasswd())) {
            return null;
        }
        return users.get(0);
    }
}
